$(function() {
	var charts = {};

	var barChart = function(elem, xData, yData, dataName, labelFormat) {
		var chart = echarts.init(elem);

		chart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			tooltip : {
				trigger : 'axis'
			},
			legend : {
				data : [ dataName ]
			},
			toolbox : {
				show : true,
				feature : {
					saveAsImage : {
						show : true
					}
				}
			},
			calculable : true,
			xAxis : [ {
				type : 'category',
				axisLabel : {
					'interval' : 0,
					rotate : 0
				},
				data : xData
			} ],
			yAxis : [ {
				type : 'value'
			} ],
			series : [ {
				name : dataName,
				type : 'bar',
				itemStyle : {
					normal : {
						label : {
							show : true,
							position : 'top',
							formatter : labelFormat
						}
					}
				},
				data : yData
			} ]
		};

		chart.setOption(option);

		chart.hideLoading();

		return chart;
	};

	var getDataAndDraw = function(selector, url, dataName, labelFormat) {
		if (!selector || !url)
			return;

		var container = $(selector);

		if (container.length <= 0)
			return;

		// get data
		$.ajax({
			async : true,
			url : url,
			type : 'POST',
			dataType : 'json',
			success : function(data, textStatus, jqXHR) {
				if (!data) {
					container.text('暂无数据');
					return;
				}

				var xData = data.xData, yData = data.yData;

				var chart = barChart(container.get(0), xData, yData, dataName, labelFormat);

				charts[selector] = chart;
			},
			error : function(qXHR, textStatus, errorThrown) {
				container.text('暂无数据');
			},
			complete : function(jqXHR, textStatus) {
			}
		});
	};

	var thisYearProjectPerformStatus = function() {
		var year = $('.panel-title input.status.current-year').val();

		if (!year)
			return;

		var selector = ".this-year-project-perform-status";

		if (charts[selector] && charts[selector].dispose) {
			charts[selector].dispose();
			charts[selector] = false;
		}

		var url = $.suredy.contextPath + '/front-page/this-year-project-perform-status/' + year;

		getDataAndDraw(selector, url, '数量', '{c}');
	};

	var thisYearOutputValue = function() {
		var year = $('.panel-title input.output-value.current-year').val();

		if (!year)
			return;

		var selector = ".this-year-output-value";

		if (charts[selector] && charts[selector].dispose) {
			charts[selector].dispose();
			charts[selector] = false;
		}

		var url = $.suredy.contextPath + '/front-page/this-year-output-value/' + year;

		getDataAndDraw(selector, url, '金额', '￥{c}');
	};

	thisYearProjectPerformStatus();
	thisYearOutputValue();

	$('.panel-title input.status.current-year').on('keydown', function(event) {
		if (event.keyCode !== 13)
			return true;

		thisYearProjectPerformStatus();
	});

	$('.panel-title input.output-value.current-year').on('keydown', function(event) {
		if (event.keyCode !== 13)
			return true;

		thisYearOutputValue();
	});
});